﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonEditor.CommandUIDefinitionClasses;

namespace RibbonEditor.Controls.CommandUIDefinitions
{
    public partial class GroupControl : UserControl, IControlRulable
    {
        private Group innerGroup;

        internal Group InnerGroup
        {
            get { return innerGroup; }
            set { innerGroup = value; }
        }

        public GroupControl()
        {
            InitializeComponent();
            this.innerGroup = new Group();
            this.FillControls();
        }

        public GroupControl(Group group):this()
        {
            this.innerGroup = group;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                this.rcCommand.Value = this.innerGroup.Command;
                this.strDescription.Value = this.innerGroup.Description;
                this.strId.Value = this.innerGroup.Id;
                this.rcImage32by32.Value = this.innerGroup.Image32by32Popup;
                this.strImage32by32Class.Value = this.innerGroup.Image32by32PopupClass;
                this.icImage32by32Left.Value = this.innerGroup.Image32by32PopupLeft;
                this.icImage32by32Top.Value = this.innerGroup.Image32by32PopupTop;
                this.strPopupWidth.Value = this.innerGroup.PopupWidth;
                this.icSequence.Value = this.innerGroup.Sequence;
                this.rcTemplate.Value = this.innerGroup.Template;
                this.strTitle.Value = this.innerGroup.Title;
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.innerGroup.Command = this.rcCommand.Value;
                    this.innerGroup.Description = this.strDescription.Value;
                    this.innerGroup.Id = this.strId.Value;
                    this.innerGroup.Image32by32Popup = this.rcImage32by32.Value;
                    this.innerGroup.Image32by32PopupClass = this.strImage32by32Class.Value;
                    this.innerGroup.Image32by32PopupLeft = this.icImage32by32Left.Value;
                    this.innerGroup.Image32by32PopupTop = this.icImage32by32Top.Value;
                    this.innerGroup.PopupWidth = this.strPopupWidth.Value;
                    this.innerGroup.Sequence = this.icSequence.Value;
                    this.innerGroup.Template = this.rcTemplate.Value;
                    this.innerGroup.Title = this.strTitle.Value;

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void GroupControl_Leave(object sender, EventArgs e)
        {
            try
            {
                if (UserControlHelper.LeaveControl(this))
                {
                    this.Save();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members


        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.innerGroup.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
